﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RadarMonitor.DB;
using RadarMonitor;


namespace RadarMonitor.ViewModel
{
    class NetMonitorViewModel : INotifyPropertyChanged
    {
        private Setting.PingConfig config;
        public NetMonitorViewModel()
        {
            config = Setting.Setting.GetConfig().ping;
            NetMonitor.NetMonitor.Instance.PingResultEvent += Instance_PingResultEvent;
        }

        private void Instance_PingResultEvent(bool online, string Descroption)
        {
            if (online)
            {
                DBHelper.Instance.insertPingNetRow((int)RadarMonitorResultCode.Success);
                ResultCode = (int)RadarMonitorResultCode.Success;
            }
            else
            {
                DBHelper.Instance.insertPingNetRow((int)RadarMonitorResultCode.PingFailed);
                DBHelper.Instance.insertMonidorRow((int)RadarMonitorSource.NetPing, (int)RadarMonitorResultCode.PingFailed,null);
                ResultCode = (int)RadarMonitorResultCode.PingFailed;
                Alarm.AlarmManager.Alarm(RadarMonitorSource.NetPing, RadarMonitorResultCode.PingFailed, null, config.DingDingEnabel, config.SmsEnabel, config.AudioEnabel);
            }
            PingDescription = Descroption;
            LastCheckTime = DateTime.Now;
        }

        #region DataContext
        private bool _isWorking;

        public bool IsWorking
        {
            get { return _isWorking; }
            set
            {
                _isWorking = value;
                OnPropertyChanged("IsWorking");
            }
        }

        private DateTime _lastCheckTime;

        public DateTime LastCheckTime
        {
            get { return _lastCheckTime; }
            set
            {
                _lastCheckTime = value;
                OnPropertyChanged("LastCheckTime");
            }
        }

        private int _resultCode;

        public int ResultCode
        {
            get { return _resultCode; }
            set
            {
                _resultCode = value;
                OnPropertyChanged("ResultCode");
            }
        }

        private string _pingDescription;

        public string PingDescription
        {
            get { return _pingDescription; }
            set { _pingDescription = value;
                OnPropertyChanged("PingDescription");
            }
        }



        #endregion

        #region Action

        public void start()
        {
            NetMonitor.NetMonitor.Instance.Start();
            IsWorking = true;
        }

        public void stop()
        {
            NetMonitor.NetMonitor.Instance.Stop();
            IsWorking = false;
        }

        public void toogle()
        {
            if (IsWorking)
            {
                stop();
            }
            else
            {
                start();
            }
        }

        #endregion

        #region 属性变化通知
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName = "")
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }



        #endregion
    }
}
